﻿using BloggerAutoPoster.Common.Mvvm;
using BloggerAutoPoster.Infrastructure.Models;
using BloggerAutoPosterService.Engine.Models;

namespace BloggerAutoPoster.Infrastructure.ViewModels
{
    public partial class PostsListViewModel
    {
        #region Fields (1)

        const int ItemsPerPage = 20;

        #endregion Fields

        #region Properties (5)

        public DelegateCommand<string> GoFirst { set; get; }

        public DelegateCommand<string> GoLast { set; get; }

        public DelegateCommand<string> GoNext { set; get; }

        public DelegateCommand<string> GoPrevious { set; get; }

        public ListViewGui ListViewGuiData { set; get; }

        #endregion Properties

        #region Methods (10)

        // Private Methods (10) 

        private bool canGoFirst(string data)
        {
            return ListViewGuiData.CurrentPage != 0;
        }

        private bool canGoLast(string data)
        {
            return ListViewGuiData.CurrentPage != ListViewGuiData.TotalPage - 1;
        }

        private bool canGoNext(string data)
        {
            return ListViewGuiData.CurrentPage < ListViewGuiData.TotalPage - 1;
        }

        private bool canGoPrevious(string data)
        {
            return ListViewGuiData.CurrentPage > 0;
        }

        private void goFirst(string data)
        {
            if (ListViewGuiData.CurrentPage == 0) return;

            _searchData = string.Empty;
            ListViewGuiData.CurrentPage = 0;
            NewsItemsDataView.MoveCurrentToFirst();
            NewsItemsDataView.Refresh();
        }

        private void goLast(string data)
        {
            if (ListViewGuiData.CurrentPage == ListViewGuiData.TotalPage - 1) return;

            _searchData = string.Empty;
            ListViewGuiData.CurrentPage = ListViewGuiData.TotalPage - 1;
            NewsItemsDataView.MoveCurrentToLast();
            NewsItemsDataView.Refresh();
        }

        private void goNext(string data)
        {
            if (ListViewGuiData.CurrentPage >= ListViewGuiData.TotalPage - 1) return;

            _searchData = string.Empty;
            ListViewGuiData.CurrentPage++;
            NewsItemsDataView.MoveCurrentToNext();
            NewsItemsDataView.Refresh();
        }

        private void goPrevious(string data)
        {
            if (ListViewGuiData.CurrentPage <= 0) return;

            _searchData = string.Empty;
            ListViewGuiData.CurrentPage--;
            NewsItemsDataView.MoveCurrentToPrevious();
            NewsItemsDataView.Refresh();
        }

        void pagerInit()
        {
            ListViewGuiData = new ListViewGui();
            GoFirst = new DelegateCommand<string>(goFirst, canGoFirst);
            GoPrevious = new DelegateCommand<string>(goPrevious, canGoPrevious);
            GoNext = new DelegateCommand<string>(goNext, canGoNext);
            GoLast = new DelegateCommand<string>(goLast, canGoLast);
        }

        void reCalculatePagerItems(int itemsCount)
        {
            if (!string.IsNullOrWhiteSpace(_searchData))
            {
                ListViewGuiData.TotalPage = 1;
                ListViewGuiData.CurrentPage = 0;
            }
            else
            {
                ListViewGuiData.TotalPage = itemsCount / ItemsPerPage;
                if (itemsCount % ItemsPerPage == 0) return;
                ListViewGuiData.TotalPage += 1;
            }
        }

        bool viewFilter(object data)
        {
            return string.IsNullOrWhiteSpace(_searchData) ? applyPaging(data) : searchData(data);
        }

        private bool applyPaging(object data)
        {
            if (data == null) return false;
            var newsItem = data as NewsRecord;
            if (newsItem == null) return false;
            var index = _newsItemsDataInternal.IndexOf(newsItem);
            return index >= ItemsPerPage * ListViewGuiData.CurrentPage && index < ItemsPerPage * (ListViewGuiData.CurrentPage + 1);
        }

        private bool searchData(object data)
        {
            if (data == null) return false;
            var newsItem = data as NewsRecord;
            return (newsItem != null) &&
                        (newsItem.Annotation.ToLower().Contains(_searchData.ToLower()) ||
                        newsItem.Title.ToLower().Contains(_searchData.ToLower()) ||
                        newsItem.AddDate.ToLower().Contains(_searchData.ToLower()));
        }

        #endregion Methods
    }
}
